<template>
  <div>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="Id" width="180">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名" width="180">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>姓名: {{ scope.row.username }}</p>
            <p>住址: {{ scope.row.address }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.username }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <div class="block">
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handlePageIndexChange"
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.rowsTotal"
        background
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getList } from "../api/users";

export default {
  data() {
    return {
      tableData: [],
      pager:{
        pageIndex:1,
        pageSize:10,
        rowsTotal:30
      }
    };
  },
  mounted() {
    this.fetchData(this.pager)
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    // 页面页码改变时，重新拉取数据
    handlePageIndexChange(val){
      console.log(val);
      this.pager.pageIndex=val
      this.fetchData(this.pager)
    },
    // 页面大小改变时，重新拉取数据
    handlePageSizeChange(val){
      console.log(val);
      this.pager.pageSize=val
      this.fetchData(this.pager)
    },
    // 拉取后台数据（带分页）
    fetchData(pager){
      getList(pager).then(({ data }) => {
      this.tableData = data.data.data;
      this.pager=data.data.pager
    });
    }
  },
};
</script>